﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Windows.Forms;

namespace function.jsonview
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.richTextBox1.Text))
            {
                string str = this.richTextBox1.Text;
                try
                {
                    var jArray = JArray.Parse(str);

                    TreeNode subNode = new TreeNode("start");

                    this.treeView1.Nodes.Add(subNode);

                    //JObject o = new JObject();
                    //o["start"] = jArray;
                    recursion(subNode, jArray);
                }
                catch (Exception ee)
                {
                    MessageBox.Show("无法识别");
                }
            }
        }

        private void recursion(TreeNode treeNode, IEnumerable jArray)
        {
            foreach (IEnumerable item in jArray)
            {
                TreeNode subNode = new TreeNode((item as JToken).ToString());
                treeNode.Nodes.Add(subNode);
                treeNode.ExpandAll();

                //this.treeView1.DataBindings

                recursion(subNode, item);
            }

            //if (jArray.Count > 0)
            //{
            //}
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
        }
    }
}